package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import zhihuilan.entity.MyCart;
import zhihuilan.entity.MyCategoryProduct;
import zhihuilan.entity.MyFactoryProduct;
import zhihuilan.mapper.MyCartMapper;
import zhihuilan.mapper.MyCategoryProductMapper;
import zhihuilan.mapper.MyCategoryStaffMapper;
import zhihuilan.mapper.MyFactoryProductMapper;
import zhihuilan.params.query.MyCategoryProductQueryParam;
import zhihuilan.service.MyCategoryProductService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MyCategoryProductServiceImpl extends ServiceImpl<MyCategoryProductMapper, MyCategoryProduct> implements MyCategoryProductService {

    @Resource
    private MyCartMapper myCartMapper;

    @Resource
    private MyFactoryProductMapper myFactoryProductMapper;

    @Override
    public List<MyCategoryProduct> selectOurFactoryProductList(MyCategoryProductQueryParam queryParam) {
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
//        Integer pid = 0;
//        String existsSql = "exists (select b.pid1 from my_factory_product b where b.status = 1 and b.shopid=" + queryParam.getShopid() + " and b.factoryid=0 and b.price|b.price_q|b.price_z > 0 and b.pid1=a.id )";
        List<MyCategoryProduct> result = new ArrayList<>();
        if (!ObjectUtils.isEmpty(queryParam.getPid1())) {
            result = this.baseMapper.selectListToPid1(queryParam.getPid1(), queryParam.getShopid());
        } else if (!ObjectUtils.isEmpty(queryParam.getPid2())) {
            result = this.baseMapper.selectListToPid2(queryParam.getPid2(), queryParam.getShopid());
        } else if (!ObjectUtils.isEmpty(queryParam.getPid3())) {
            result = this.baseMapper.selectListToPid3(queryParam.getPid3(), queryParam.getShopid());
        } else if (!ObjectUtils.isEmpty(queryParam.getPid4())) {
            result = this.baseMapper.selectListToPid4(queryParam.getPid4(), queryParam.getShopid());
        } else {
            result = this.baseMapper.selectListToPid0(0, queryParam.getShopid());
        }
        return result;
    }

    @Override
    public List<MyCategoryProduct> selectList(MyCategoryProductQueryParam queryParam) {
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        QueryWrapper<MyCategoryProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.eq("isdelete", 0);
        queryWrapper.eq("factoryid", 0);

        if (!ObjectUtils.isEmpty(queryParam.getName())) {
            queryWrapper.eq("name", queryParam.getName());
        }
        if (!ObjectUtils.isEmpty(queryParam.getPid())) {
            queryWrapper.eq("pid", queryParam.getPid());
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            queryWrapper.nested(wp -> wp.eq("shopid", 0).or().eq("shopid", queryParam.getShopid()));
        }
        if (!ObjectUtils.isEmpty(queryParam.getOrderByFiled())) {
            queryWrapper.orderByAsc(queryParam.getOrderByFiled());
        } else {
            queryWrapper.orderByAsc("sort");
        }
        List<MyCategoryProduct> result = this.baseMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public MyCategoryProduct selectById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return null;
        }

        return this.baseMapper.selectById(id);
    }

    @Transactional
    @Override
    public MyCategoryProduct addEntity(MyCategoryProduct entity) throws RuntimeException {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        entity.setAddtime(new Date());
        this.baseMapper.insert(entity);
        if (entity.getPid().equals(0)) {
            if (ObjectUtils.isEmpty(entity.getSortpath())) {
                entity.setSortpath("0," + entity.getId() + ",");
            }
        } else {
            String sortpath = this.getParentSortPath(entity.getPid());
            entity.setSortpath(sortpath + entity.getId() + ",");
        }
        if (ObjectUtils.isEmpty(entity.getDepth())) {
            entity.setDepth(entity.getSortpath().split(",").length - 1);
        }
        if (ObjectUtils.isEmpty(entity.getSort())) {
            entity.setSort(entity.getId());
        }
        this.baseMapper.updateById(entity);
        if (entity.getPid() > 0) {
            MyCategoryProduct parentEntity = this.baseMapper.selectById(entity.getPid());
            QueryWrapper<MyCategoryProduct> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid", entity.getPid());
            queryWrapper.ne("pid", 0);
            Long count = this.baseMapper.selectCount(queryWrapper);

            if (!ObjectUtils.isEmpty(parentEntity)) {
                parentEntity.setFchild(Integer.parseInt(count + ""));
            }
            this.baseMapper.updateById(parentEntity);
        }
        return entity;
    }

    private String getParentSortPath(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return "";
        }
        MyCategoryProduct entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            return "";
        } else {
            return entity.getSortpath();
        }
    }

    @Override
    public MyCategoryProduct updateEntity(MyCategoryProduct entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (ObjectUtils.isEmpty(entity.getId()) || entity.getId() == 0) {
            return null;
        }
        this.baseMapper.updateById(entity);
        return null;
    }

    @Override
    public Boolean deletetById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return false;
        }
        int delete = this.baseMapper.deleteById(id);

        QueryWrapper<MyCategoryProduct> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.like("sortpath", id);
        delete = this.baseMapper.delete(deleteWrapper);
        return delete > 0;
    }

    @Override
    public Boolean deleteToAll(MyCategoryProduct deleteEntity) {
        if (ObjectUtils.isEmpty(deleteEntity)) {
            return false;
        }
        if (ObjectUtils.isEmpty(deleteEntity.getId())) {
            return false;
        }
        int deleteResult = this.baseMapper.deleteById(deleteEntity.getId());
        QueryWrapper<MyCategoryProduct> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.like("sortpath", deleteEntity.getId());
        deleteResult += this.baseMapper.delete(deleteWrapper);
        Integer depth = deleteEntity.getDepth();

        QueryWrapper<MyCart> deleteCartWrapper = new QueryWrapper<>();
        QueryWrapper<MyFactoryProduct> deleteFProductWrapper = new QueryWrapper<>();
        switch (depth) {
            case 1:
                deleteCartWrapper = new QueryWrapper<>();
                deleteCartWrapper.eq("pid1", deleteEntity.getId());
                deleteResult += myCartMapper.delete(deleteCartWrapper);
                deleteFProductWrapper = new QueryWrapper<>();
                deleteFProductWrapper.eq("pid1", deleteEntity.getId());
                deleteResult += myFactoryProductMapper.delete(deleteFProductWrapper);
                break;
            case 2:
                deleteCartWrapper = new QueryWrapper<>();
                deleteCartWrapper.eq("pid2", deleteEntity.getId());
                deleteResult += myCartMapper.delete(deleteCartWrapper);
                deleteFProductWrapper = new QueryWrapper<>();
                deleteFProductWrapper.eq("pid2", deleteEntity.getId());
                deleteResult += myFactoryProductMapper.delete(deleteFProductWrapper);
                break;
            case 3:
                deleteCartWrapper = new QueryWrapper<>();
                deleteCartWrapper.eq("pid3", deleteEntity.getId());
                deleteResult +=myCartMapper.delete(deleteCartWrapper);
                deleteFProductWrapper = new QueryWrapper<>();
                deleteFProductWrapper.eq("pid3", deleteEntity.getId());
                deleteResult += myFactoryProductMapper.delete(deleteFProductWrapper);
                break;
            case 4:
                deleteCartWrapper = new QueryWrapper<>();
                deleteCartWrapper.eq("pid4", deleteEntity.getId());
                deleteResult +=myCartMapper.delete(deleteCartWrapper);
                deleteFProductWrapper = new QueryWrapper<>();
                deleteFProductWrapper.eq("pid4", deleteEntity.getId());
                deleteResult += myFactoryProductMapper.delete(deleteFProductWrapper);
                break;
            case 5:
                deleteCartWrapper = new QueryWrapper<>();
                deleteCartWrapper.eq("pid5", deleteEntity.getId());
                deleteResult +=myCartMapper.delete(deleteCartWrapper);
                deleteFProductWrapper = new QueryWrapper<>();
                deleteFProductWrapper.eq("pid5", deleteEntity.getId());
                deleteResult += myFactoryProductMapper.delete(deleteFProductWrapper);
                break;
        }
        return deleteResult > 0;
    }

    @Override
    public List<MyCategoryProduct> selectListToShopId(Integer shopId) {
        return this.baseMapper.selectListToShopId(shopId);
    }
}
